package com.atguigu.spring5.aopanno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

// 增强的类
@Component
@Aspect // 生成代理对象
@Order(3)
public class UserProxy {

    // 相同切入点抽取
    @Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.*(..))")
    public void pointdemo() {
    }

    // 前置通知
    @Before(value = "pointdemo()")
    // @Before(value = "execution(* com.atguigu.spring5.aopanno.User.*(..))")
    public void before() {
        System.out.println("before......");
    }

    // 后置通知（返回通知）
    @AfterReturning(value = "pointdemo()")
    // @AfterReturning(value = "execution(* com.atguigu.spring5.aopanno.User.*(..))")
    public void afterReturning() {
        // 返回值之后执行
        // 有异常不执行
        System.out.println("afterReturning......");
    }

    // 最终通知
    @After(value = "pointdemo()")
    // @After(value = "execution(* com.atguigu.spring5.aopanno.User.*(..))")
    public void after() {
        // 方法之后执行
        // 有无异常都执行
        System.out.println("after......");
    }

    // 异常通知
    @AfterThrowing(value = "pointdemo()")
    // @AfterThrowing(value = "execution(* com.atguigu.spring5.aopanno.User.*(..))")
    public void afterThrowing() {
        System.out.println("afterThrowing......");
    }

    // 环绕通知
    @Around(value = "pointdemo()")
    // @Around(value = "execution(* com.atguigu.spring5.aopanno.User.*(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前......");
        // 被增强的方法执行
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后......");
    }
}
